/**
 * Theme Plugins
 * @author ZhangHuihua@msn.com
 */
(function($){
	$.fn.extend({
		cssTable: function(options){
			var op = $.extend({scrollBox:"tableList"}, options);
			return this.each(function(){
				var $this = $(this);
				var $trs = $this.find("tbody>tr:not('tbody>tr.hide,tbody>tr.nodate')");
				if (!$this.parent().hasClass(op.scrollBox)){
					var lh = $this.attr('layoutH');
					$this.removeAttr('layoutH');
					$this.wrap('<div class="'+op.scrollBox+'"'+ (lh ? ' layoutH="'+lh+'"' : '') +'></div>');
				}
				//$this.parents(".pageContent").css("overflow","hidden");
				
				var $grid = $this.parent(); // table scrollBox
				
				$trs.hover(function(){
					$(this).addClass('hover');
				}, function(){
					$(this).removeClass('hover');
				}).each(function(index){
					var $tr = $(this);
					if (index % 2 == 1) $tr.addClass("trbg");
					
					//单击行
					$tr.unbind("click").click(function(){
						if($tr.hasClass("selected")) return;
						//$tr.parents(".tabsPageContent").find("tr.selected").removeClass("selected");
						//$trs.filter(".selected").not($tr).removeClass("selected");
						//$tr.toggleClass("selected");
						$tr.addClass("selected");
						$tr.parents(".tabsPageContent,table.list").find("tr.selected").not($tr).removeClass("selected");
						var sTarget = $tr.attr("target");
						if (sTarget) {
							if ($("#"+sTarget, $grid).size() == 0) {
								$grid.prepend('<input id="'+sTarget+'" type="hidden" />');
							}
							$("#"+sTarget, $grid).val($tr.attr("rel"));
						}
					});
					
					//双击td编辑
					$(">td.edit",this).each(function(){
						var td=$(this);
						var title=td.attr("title");
						td.mousemove(function(e){
							var l=(e.offsetX==undefined?(e.layerX-td[0].offsetLeft):e.offsetX);
							var off = td.width()-l;
							td.css("cursor",off<0?"pointer":"default");
							if(off<0){
								td.attr("title","编辑："+title);
							}else{
								td.attr("title",title);
							}
						}).mouseout(function(e){
							td.attr("title",title);
						}).click(function(e){
							if((e.srcElement||e.target).tagName.toLowerCase()=="input") return;
							var l=(e.offsetX==undefined?(e.layerX-td[0].offsetLeft):e.offsetX);
							var off = td.width()-l;
							if(off<0){
								editTb(e);
							}
						}).dblclick(function(e){
							editTb(e);
						});
						
						var text=td.html().trim();
						
						function editTb(e){							
							var type=td.attr("type");
							if(type)type = type.toLowerCase();
							switch(type){
								case "editor":editor(e,"editor");break;
								case "upload":upload(e);break;
								case "textarea":editor(e,"textarea");break;
								case "entity":selectCategory(e);break;
								default:edit(e);
							}
						}
						
						function selectCategory(e){
							var sign = td.attr("sign");
							var options={
								width:300,
								height:450,
								event:e
							};
							$.pdialog.open("category/select.html?type=treeRadio&sign="+sign, "category", "选择分组：", options,function(category){
								put(category.category_id);
							})
						}
						
						function put(value){
							value=""+value;
							td.attr("title",value);
							if(value!=text.trim()){
								var url=$tr.attr("put");
								var field=td.attr("field")||td.attr("name");
								if(url&&field){
									var pars={};
									pars[field]=value;
									DWZ.put(url,pars,function(json){
										td.html(value.encodeHTML());
									})
								}else{
									td.html(value.encodeHTML());
								}
							}else{
								td.html(value.encodeHTML());
							}
						}
						
						function editor(e,type){
							var content = $.trim(td.html());
							var dialog = function(){
								var options={
									width:640,
									height:430,
									event:e,
									opened:function(jDom){
										//var html=text.decodeHTML();
										jDom.find("textarea."+type).val(content);
									}
								};
								$.pdialog.open(type+".html", "editor", "编辑：", options,function(data){
									var pars={};
									var field=td.attr("field")||td.attr("name");
									pars[field]=data.content;
									pars.xhe_files = data.xhe_files;
									DWZ.put($tr.attr("put"),pars,function(json){
										$.get(url,function(json){
											td.text(json.result.summary).attr("title",json.result.summary);
										});
									})
									//put(value);
								});
							}
							
							type=type||"editor";
							url = td.attr("url");
							if(url){
								DWZ.get(url,function(json){
									content = json.result.content;
									dialog();
								});
							}else{
								dialog();
							}
						}
						
						function upload(e){
							
						}
						
						function edit(e){
							if($(">input.edit",td).length>0) return;
							td.attr("style","padding:0px");
							
							var input="<input type='text' value='"+text+"' title='按ESC键取消编辑' alt='请输入内容后回车自动保存！' />";
							var $input=$(input);//.tips();
							td.html("");
							$input.appendTo(td);
							$input.inputAlert();
							$input.focus();
							try{
								var oRng=$input[0].createTextRange();
								if(oRng){
									oRng.move('character',text.length);
									oRng.collapse(true);
									oRng.select();
								}
							}catch(e){}
							//$input.select();
							$input.blur(function(event){
								var value = (this.value||"").trim();
								put(value);
								$input.remove();
								td.removeAttr("style");
							});
							$input.keydown(function(event){
								if(event.keyCode==27){$input.val(text);$input.blur();}
								if(event.keyCode==13) $input.blur();
							});
						}
					});
				});
				
				$(">thead input[name=objectId]",$this).click(function(){
					var $o = $(this);
					
					if($o.hasClass("filter30")){
						this.checked=true;
					}
					
					if(this.checked){
						$("tbody.list>tr>td>input[name="+this.value+"]:not('input:disabled')",$this).attr("checked","checked").parents("tr").addClass("checked");
					}else{
						$("tbody.list>tr>td>input[name="+this.value+"]",$this).removeAttr("checked").parents("tr").removeClass("checked");
					}
					$o.removeClass("filter30");
				});
				
				$(">tbody.list>tr>td>input:checkbox:not(':disabled')",$this).click(function(){
					var all = $(">tbody.list>tr>td>input:checkbox:not(':disabled')",$this);
					var checked = $(">tbody.list>tr>td>input[checked]:checkbox:not(':disabled')",$this);
					var a = $(">thead input[name=objectId]",$this);
					if(all.length==checked.length){
						a.removeClass("filter30").attr("checked","checked");
					}else if(checked.length==0){
						a.removeClass("filter30").removeAttr("checked");
					}else{
						a.addClass("filter30").attr("checked","checked");
					}
					
					if(this.checked===true){
						$(this).parents("tr").addClass("checked");
					}else{
						$(this).parents("tr").removeClass("checked");
					}
				});
			});
		}
	});
})(jQuery);
